package com.mjw.tender.service;

import com.mjw.tender.api.ArticleService;
import com.mjw.tender.api.FavouriteService;
import com.mjw.tender.domain.Article;
import com.mjw.tender.domain.Favourite;
import com.mjw.tender.mapper.ArticleMapper;
import com.mjw.tender.mapper.CommentMapper;
import com.mjw.tender.mapper.FavouriteMapper;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service(version = "1.0.0")
public class FavouriteServiceImpl implements FavouriteService {
    @Resource
    FavouriteMapper favouriteMapper;

    @Resource
    ArticleMapper articleMapper;

    @Resource
    ArticleService articleService;

    @Resource
    CommentMapper commentMapper;

    @Override
    @Transactional
    public Long add(Favourite favourite) {
        favouriteMapper.insertSelective(favourite);
        int update = 0;
        if(favourite.getResourceType() == 1){   // 文章相关操作
             update = articleMapper.increaseLikeOrCollect(favourite.getArticleId(),favourite.getActionType());
        }else if(favourite.getResourceType() ==2){ //评论相关操作
             update = commentMapper.increaseLike(favourite.getCommentId());
        }
        return favourite.getId();
    }

    @Override
    public List<Favourite> getByArticleIdAndUserId(Long articleId, Long userId) {
        Example example = new Example(Favourite.class);
        example.createCriteria().andEqualTo("articleId",articleId).andEqualTo("userId",userId);
        List list = new ArrayList<Favourite>(favouriteMapper.selectByExample(example));
        return list;
    }

    @Override
    @Transactional
    public boolean remove(Long articleId, String userName, Short actionType) throws Exception{
        Example example = new Example(Favourite.class);
        example.createCriteria().andEqualTo("articleId",articleId)
                .andEqualTo("userName",userName).andEqualTo("actionType",actionType);
        int delete =  favouriteMapper.deleteByExample(example);
        if(delete<1){ //删除失败
            throw new Exception("com.mjw.tender.service.FavouriteServiceImpl.remove()->delete=0");
        }
        Article article = articleService.get(articleId);
        if(actionType == 1){
            article.setLikeCount(article.getLikeCount()-1);
        }else if(actionType ==2){
            article.setCollectCount(article.getCollectCount()-1);
        }
        int update = articleService.update(article);
        if(update<1){//更新失败
            throw new Exception("com.mjw.tender.service.FavouriteServiceImpl.remove()->update=0");
        }
        return true;
    }

    @Override
    public Integer getTotal() {
        Example example =new Example(Favourite.class);
        return favouriteMapper.selectCountByExample(example);
    }


    /**
     * 涉及并发更新操作，点赞数量不重要所以不控制，需要准确控制点赞数时用分布式锁
     * @param commentId
     * @param userName
     * @return
     */
    @Override
    @Transactional
    public int removeCommentLike(Long commentId, String userName) {
        Example example = new Example(Favourite.class);
        example.createCriteria().andEqualTo("commentId",commentId).andEqualTo("userName",userName);
        int update = commentMapper.decreaseLike(commentId);
        int delete = favouriteMapper.deleteByExample(example);
        return delete;
    }
}
